﻿using ApiLib.AppStart;
using ApiLib.Utils;
using SqlSugar;

namespace ApiLib.Models;

/// <summary>
/// 基础数据库连接配置
/// </summary>
public class BaseConnectionConfig
{
    public dynamic ConfigId { get; set; }

    /// <summary>
    /// 连接方式
    /// </summary>
    public DbType DbType { get; set; } = DbType.MySql;

    /// <summary>
    /// 数据库
    /// </summary>
    public string ConnectionString { get; set; } = "Mysql";

    /// <summary>
    /// 事件
    /// </summary>
    public AopEvents AopEvents { get; set; } = new()
    {
        OnLogExecuting = GlobalConfig.AppConfig != null && GlobalConfig.AppConfig.IsProd
            ? null
            : (sql, pas) =>
            {
                WebUtil.LogSql(sql);
                MongoUtil.LogSql(sql);
                if (GlobalConfig.AppConfig!=null && GlobalConfig.AppConfig.IsDebug)
                {
                    foreach (var pa in pas)
                    {
                        Console.WriteLine($"参数：{pa.ParameterName}->{pa.DbType}->{pa.Value}");
                    }
                }
            },
        OnError = exception =>
        {
            var msg = $"执行sql：{exception.Sql}，报错：{exception.Message}";
            msg.LogError();
            MongoUtil.LogExp(msg);
        }
    };
}